package com.joysuch.wwyt.bp.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.bp.bean.investigate.TaskSummarySearchBean;
import com.joysuch.wwyt.bp.bean.investigate.vo.AppInvestigateSummaryVo;
import com.joysuch.wwyt.bp.bean.vo.RiskPointSnapshotVo;
import com.joysuch.wwyt.bp.entity.BpInvestigateTaskSummary;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.joysuch.wwyt.bp.entity.bean.RecordSearchBean;
import com.joysuch.wwyt.bp.entity.dto.UserTaskRecordDto;
import com.joysuch.wwyt.bp.entity.dto.BpInvestigateTaskSummaryDto;
import com.joysuch.wwyt.bp.entity.dto.CheckRecordDto;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigateRecordItem;
import com.joysuch.wwyt.bp.enums.UserUnFinishTaskCount;
import com.joysuch.wwyt.core.bean.SimpleListBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import javax.xml.crypto.Data;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author zr
 * @since 2021-09-16
 */
@Repository
@Mapper
public interface BpInvestigateTaskSummaryMapper extends BaseMapper<BpInvestigateTaskSummary> {
    List<CheckRecordDto> getRecordByPaging(Page<CheckRecordDto> page, @Param("recordSearchBean") RecordSearchBean recordSearchBean, @Param("riskMeasureTypeIds") List<Long> riskMeasureTypeIds);

    List<CheckRecordDto> getHistoryRecordByPaging(Page<CheckRecordDto> page, @Param("recordSearchBean") RecordSearchBean recordSearchBean, @Param("riskMeasureTypeIds") List<Long> riskMeasureTypeIds);

    List<CheckRecordDto> getRecordByPaging(@Param("recordSearchBean") RecordSearchBean recordSearchBean);

    List<CheckRecordDto> getSimpleRecordByPaging(@Param("recordSearchBean") RecordSearchBean recordSearchBean);

    List<CheckRecordDto> getTaskRecordByIds(@Param("list") List<Long> list);

    /**
     * 获取一个任务汇总中所有相关的风险点
     * @param searchBean
     * @return
     */
    List<RiskPointSnapshotVo> getRiskPoint(@Param("searchBean")RecordSearchBean searchBean);

    /**
     * 获取一个汇总中记录的任务id(区分历史和今日)
     */
    List<Long> getTaskIdsByDateAndUser(@Param("start") Date start,@Param("end") Date end,@Param("userId") Long userId,@Param("isHistory") boolean isHistory);

    /**
     * 获取指定任务相关的风险点(区分历史和今日)
     */
    List<RiskPointSnapshotVo> getRiskPointByTaskIds(@Param("taskIds")List<Long> taskIds, boolean isHistory);

    long getRecordCount(@Param("recordSearchBean") RecordSearchBean recordSearchBean, @Param("riskMeasureTypeIds") List<Long> riskMeasureTypeIds);

    long getHistoryRecordCount(@Param("recordSearchBean") RecordSearchBean recordSearchBean, @Param("riskMeasureTypeIds") List<Long> riskMeasureTypeIds);

    List<SimpleSelectListBean> getSubscript(@Param("recordSearchBean") RecordSearchBean recordSearchBean);

    IPage<BpInvestigateTaskSummaryDto> findRecordSummaryByPaging(Page<BpInvestigateTaskSummary> page, Long userId, @Param("searchBean") TaskSummarySearchBean searchBean, String companyCode, Long tenantId);
    Page<BpInvestigateTaskSummaryDto> findHisRecordSummaryPage(Page<BpInvestigateTaskSummary> page, @Param("searchBean") TaskSummarySearchBean searchBean, String companyCode, Long tenantId);

    long findRecordSummaryCount(Long userId, @Param("searchBean") TaskSummarySearchBean searchBean, String companyCode, Long tenantId);

    IPage<BpInvestigateTaskSummaryDto> findTaskSummaryByPaging(Page<BpInvestigateTaskSummary> page, Long userId, @Param("searchBean") TaskSummarySearchBean searchBean, String companyCode, Long tenantId);

    Page<BpInvestigateTaskSummaryDto> findTodaySummaryPage(Page<BpInvestigateTaskSummary> page, @Param("searchBean") TaskSummarySearchBean searchBean, String companyCode, Long tenantId);

    int findTodaySummaryCount(@Param("searchBean") TaskSummarySearchBean searchBean, String companyCode, Long tenantId);

    /**
     * 分级管控（source_type = 1）需要检验时间，只查当天的，不管完成状态
     * 隐患排查表（source_type = 0） 只需要校验完成状态，不查询时间
     * @param page
     * @param userId
     * @param searchBean
     * @return
     */
    Page<AppInvestigateSummaryVo> findTodaySummaryPageApp(Page<BpInvestigateTaskSummary> page, @Param("userId") Long userId, @Param("searchBean") TaskSummarySearchBean searchBean);

    /**
     * 分级管控（source_type = 1）需要检验时间，只查当天的，不管完成状态
     * 隐患排查表（source_type = 0） 只需要校验完成状态，不查询时间
     * @param userId
     * @param searchBean
     * @param companyCode
     * @param tenantId
     * @return
     */
    long findTaskSummaryCount(Long userId, @Param("searchBean") TaskSummarySearchBean searchBean, String companyCode, Long tenantId);

    List<UserUnFinishTaskCount> countUsersTaskByDeadlineAndStatus(Set<Long> assignUser, String status, Date startTime, Date endTime);

    List<UserTaskRecordDto> findUserTaskByDeadline(Long assignUser, Date startTime, Date endTime);

    List<UserTaskRecordDto> findTaskBySummaryIdOrderByFinishDate(Long summaryId);

    List<Long> findAssignUserListTaskByDeadline(Date startTime, Date endTime);

    BpInvestigateTaskSummary findByUserIdAndCheckDate(Long userId,Date checkDate);

    /**
     * 获取风险点下的最后一次隐患排查记录(带有位置校验信息)
     * @param pointId
     * @return
     */
    CheckRecordDto findLastPositionCheckRecord(Long pointId,List<Long> taskIds,boolean isHistory);

    long deleteUselessSummaryByDate(Date date);

    List<BpInvestigateTaskSummary> findSummaryByDate(Date date);

    Long getIdByDateAndUserId(Date startTime, Date endTime, Long userId);

    BpInvestigateTaskSummary getByDateAndUserId(Date startTime, Date endTime, Long userId);

    List<BpInvestigateTaskSummary> getByDateAndUserIds(Date startTime, Date endTime, List<Long> userIds);

    List<UserTaskRecordDto> findTaskByDateOrderByFinishDate(Date startTime, Date endTime, Long assign);

    List<UserTaskRecordDto> findTaskByDateAndUserIdsOrderByFinishDate(Date startTime, Date endTime, List<Long> assigns);

    List<String> findExceptionProcessResult(Date startTime, Date endTime, Long assign, Long itemId);

    void updateCheckStateById(Long id,String checkStatus);

    List<BpInvestigateTaskSummary> getByTodayTaskId(Long taskId);

    List<BpInvestigateTaskSummary> getByHisTakId(Long taskId);

    List<BpInvestigateTaskSummary> getByHistoryTaskId(Long taskId);

    List<BpInvestigateTaskSummary>  selectSummaryList(Long taskId);

    int updateExceptionProcessResultBatchById(String exceptionProcessResult, List<Long> ids);

    List<SimpleListBean> getSimplePointByIds(@Param("pointIds") Set<Long> pointIds);

    boolean updateSummaryTaskDoneCount(@Param("taskIds") Set<Long> taskIds, @Param("userId") Long userId);

    boolean updateSummaryExceptionCountAndStatus(@Param("taskIds") Set<Long> taskIds, @Param("userId") Long userId);

    boolean updateSummaryExceptionStatus(@Param("taskIds")Set<Long> taskIds,@Param("status") String status);

    boolean updateSummaryTimeOutCheckCount(@Param("taskIds")Set<Long> taskIds);

    boolean updateSummaryCompleteDateAndResult(@Param("taskIds")Set<Long> taskIds,@Param("userId") Long userId,
                                               @Param("date") Date date,@Param("result") String result);

    boolean updateSummaryStatus(@Param("taskId") Long taskId, @Param("status") String status);

    boolean updateSummaryStatusBatch(@Param("taskIds") List<Long> taskIds, @Param("status") String status);

    /**
     * 排查表隐患排查，更新汇总的异常处理状态
     */
    void updateRepairStatusByTaskId(@Param("status") String status, @Param("taskId") Long taskId);

    List<Long> getIdByDate(@Param("date") Date date);

    long copySummaryToHistoryByIds(@Param("list") Collection<Long> list);

    long deleteSummaryByIds(@Param("list") Collection<Long> list);

    List<Long> exitsIds(@Param("ids") List<Long> summaryIds);

    void updateSummaryTaskCount(@Param("taskIds") Set<Long> taskIds, @Param("userId") Long userId);

    void subSummaryTaskDoneCount(@Param("taskId") Long taskId, @Param("userId") Long userId);

    void subHisSummaryTaskDoneCount(@Param("taskId") Long taskId, @Param("userId") Long userId);

    void subSummaryExceptionCount(@Param("taskId") Long taskId, @Param("userId") Long userId);

    void subHisSummaryExceptionCount(@Param("taskId") Long taskId, @Param("userId") Long userId);

}
